import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
var _excluded = ["__id", "_status", "tenantName"];
import { CPAAS_MSG, VERSION_IS_OP } from 'utils/config';
import { getCurrentOrganizationId, isTenantRoleLevel, encryptPwd } from 'utils/utils';
import intl from 'utils/intl';
import { CODE_UPPER } from 'utils/regExp';
var organizationId = getCurrentOrganizationId();

function codeValidator(value) {
  if (CODE_UPPER.test(value)) {
    return true;
  }

  return intl.get('cpaas.common.validation.codeUpper').d('全大写及数字，必须以字母、数字开头，可包含“-”、“_”、“.”、“/”');
}
/**
 * TODO: c7n中，querybar='bar'时，queryFileds使用lov时，bind字段也会出现，后期c7n会修复
 */


var tableDs = {
  selection: false,
  autoQuery: true,
  queryFields: [!isTenantRoleLevel() && {
    name: 'organizationId',
    type: 'object',
    label: intl.get('entity.tenant.name').d('租户名称'),
    lovCode: 'CPFM.TENANT',
    noCache: true // ignore: 'always',

  }, // !isTenantRoleLevel() && {
  //   name: 'organizationId',
  //   type: 'string',
  //   bind: 'tenantLov.tenantId',
  // },
  {
    name: 'serverCode',
    type: 'string',
    label: intl.get('cmsg.common.view.serverCode').d('配置编码')
  }, {
    name: 'serverName',
    type: 'string',
    label: intl.get('cmsg.common.view.serverName').d('配置名称')
  }, {
    name: 'authType',
    type: 'string',
    label: intl.get('cmsg.dingTalkConfig.model.dingTalkConfig.authType').d('授权类型'),
    lookupCode: 'CMSG.DINGTALK.AUTH_TYPE'
  }].filter(Boolean),
  fields: [{
    name: 'tenantName',
    type: 'string',
    label: intl.get('entity.tenant.name').d('租户名称')
  }, {
    name: 'serverCode',
    type: 'string',
    label: intl.get('cmsg.common.view.serverCode').d('配置编码')
  }, {
    name: 'serverName',
    type: 'string',
    label: intl.get('cmsg.common.view.serverName').d('配置名称')
  }, {
    name: 'authTypeMeaning',
    type: 'string',
    label: intl.get('cmsg.dingTalkConfig.model.dingTalkConfig.authType').d('授权类型')
  }, {
    name: 'appKey',
    type: 'string',
    label: intl.get('cmsg.dingTalkConfig.model.dingTalkConfig.appKey').d('应用Key')
  }, isTenantRoleLevel() && !VERSION_IS_OP && {
    name: 'tenantId',
    type: 'string',
    label: intl.get('cmsg.common.view.source').d('来源')
  }, {
    name: 'enabledFlag',
    type: 'boolean',
    label: intl.get('cmsg.common.view.enabledFlag').d('启用标识'),
    trueValue: 1,
    falseValue: 0,
    defaultValue: 1
  }],
  transport: {
    /**
     * 查询钉钉配置
     */
    read: function read(config) {
      var data = config.data;
      var url = isTenantRoleLevel() ? "".concat(CPAAS_MSG, "/v1/").concat(organizationId, "/dingtalk-servers") : "".concat(CPAAS_MSG, "/v1/dingtalk-servers");
      return {
        url: url,
        method: 'GET',
        data: _objectSpread(_objectSpread({}, data), {}, {
          organizationId: data.organizationId && data.organizationId.tenantId
        })
      };
    },

    /**
     * 删除钉钉配置
     * @param {Array} data - 勾选的数据
     * @param {Array} params.permissionList = ['cmsg.ding-talk-config.delete']
     */
    destroy: function destroy(_ref) {
      var data = _ref.data;
      var url = isTenantRoleLevel() ? "".concat(CPAAS_MSG, "/v1/").concat(organizationId, "/dingtalk-servers") : "".concat(CPAAS_MSG, "/v1/dingtalk-servers");
      return {
        data: data[0],
        url: url,
        method: 'DELETE'
      };
    }
  }
};
var detailDs = {
  autoQueryAfterSubmit: false,
  fields: [!isTenantRoleLevel() && {
    name: 'tenantIdLov',
    type: 'object',
    label: intl.get('entity.tenant.name').d('租户名称'),
    lovCode: 'CPFM.TENANT',
    required: true,
    textField: 'tenantName',
    ignore: 'always',
    noCache: true
  }, !isTenantRoleLevel() && {
    name: 'tenantName',
    type: 'string',
    bind: 'tenantIdLov.tenantName',
    required: true
  }, !isTenantRoleLevel() && {
    name: 'tenantId',
    type: 'string',
    bind: 'tenantIdLov.tenantId',
    required: true
  }, {
    name: 'serverCode',
    type: 'string',
    label: intl.get('cmsg.common.view.serverCode').d('配置编码'),
    required: true,
    maxLength: 30,
    validator: codeValidator
  }, {
    name: 'serverName',
    type: 'string',
    label: intl.get('cmsg.common.view.serverName').d('配置名称'),
    required: true,
    maxLength: 30
  }, {
    name: 'authType',
    type: 'string',
    label: intl.get('cmsg.dingTalkConfig.model.dingTalkConfig.authType').d('授权类型'),
    lookupCode: 'CMSG.DINGTALK.AUTH_TYPE',
    required: true
  }, {
    name: 'appKey',
    type: 'string',
    label: intl.get('cmsg.dingTalkConfig.model.dingTalkConfig.corpid').d('应用Key'),
    dynamicProps: function dynamicProps(_ref2) {
      var record = _ref2.record;

      if (record.get('authType') === 'DingTalk') {
        return {
          required: true
        };
      }
    },
    maxLength: 60
  }, {
    name: 'appSecret',
    type: 'string',
    label: intl.get('cmsg.dingTalkConfig.model.dingTalkConfig.appSecret').d('应用密钥'),
    maxLength: 240
  }, {
    name: 'agentId',
    type: 'number',
    label: intl.get('cmsg.dingTalkConfig.model.dingTalkConfig.agentId').d('默认应用ID'),
    maxLength: 240
  }, {
    name: 'authAddress',
    type: 'string',
    label: intl.get('cmsg.dingTalkConfig.model.dingTalkConfig.authAddress').d('第三方授权地址'),
    dynamicProps: function dynamicProps(_ref3) {
      var record = _ref3.record;

      if (record.get('authType') === 'Third') {
        return {
          required: true
        };
      }
    },
    maxLength: 240
  }, {
    name: 'enabledFlag',
    type: 'boolean',
    label: intl.get('cmsg.common.view.enabledFlag').d('启用标识'),
    trueValue: 1,
    falseValue: 0,
    defaultValue: 1,
    required: true
  }],
  transport: {
    /**
     * 新建钉钉配置明细
     * @param {Array} data - 参数
     * @param {Array} params.permissionList = ['cmsg.ding-talk-config.create','cmsg.ding-talk-config.copy']
     */
    create: function create(_ref4) {
      var config = _ref4.config,
          data = _ref4.data,
          dataSet = _ref4.dataSet;

      var _data = _slicedToArray(data, 1),
          _data$ = _data[0],
          __id = _data$.__id,
          _status = _data$._status,
          tenantName = _data$.tenantName,
          rest = _objectWithoutProperties(_data$, _excluded);

      var publicKey = dataSet.queryParameter.publicKey;

      var newData = _objectSpread({}, rest);

      if (rest.appSecret) {
        newData.appSecret = encryptPwd(rest.appSecret, publicKey);
      }

      var url = isTenantRoleLevel() ? "".concat(CPAAS_MSG, "/v1/").concat(organizationId, "/dingtalk-servers") : "".concat(CPAAS_MSG, "/v1/dingtalk-servers");
      return _objectSpread(_objectSpread({}, config), {}, {
        url: url,
        method: 'POST',
        data: _objectSpread({}, newData)
      });
    },

    /**
     * 更新钉钉配置明细
     * @param {Array} data - 勾选的数据
     * @param {Array} params.permissionList = ['cmsg.ding-talk-config.edit']
     */
    update: function update(_ref5) {
      var config = _ref5.config,
          data = _ref5.data,
          dataSet = _ref5.dataSet;
      var newData = Array.isArray(data) ? data[0] : {};
      var publicKey = dataSet.queryParameter.publicKey;

      if (newData.appSecret) {
        newData.appSecret = encryptPwd(newData.appSecret, publicKey);
      }

      var url = isTenantRoleLevel() ? "".concat(CPAAS_MSG, "/v1/").concat(organizationId, "/dingtalk-servers") : "".concat(CPAAS_MSG, "/v1/dingtalk-servers");
      return _objectSpread(_objectSpread({}, config), {}, {
        url: url,
        method: 'PUT',
        data: newData
      });
    },

    /**
     * 查询钉钉配置明细
     * @param {Array} data - 勾选的数据
     * @param {Array} params.permissionList = ['cmsg.ding-talk-config.edit']
     */
    read: function read(_ref6) {
      var dataSet = _ref6.dataSet;
      var serverId = dataSet.serverId;
      var url = isTenantRoleLevel() ? "".concat(CPAAS_MSG, "/v1/").concat(organizationId, "/dingtalk-servers/").concat(serverId) : "".concat(CPAAS_MSG, "/v1/dingtalk-servers/").concat(serverId);
      return {
        url: url,
        method: 'GET',
        params: ''
      };
    }
  }
};
export { tableDs, detailDs };